define(function(require, exports){
	// var $ = require("jquery");
	var pubjs = require('pubjs');
	var view  = require('@base/view');
	var util = require('util');

	// 人群标签列表
	var List = view.container.extend({
		init: function( config ) {
			config = pubjs.conf(config, {
				'class': 'P-peopleTag',
				'urlStart': 'PDMP/recover',
				'urlPause': 'PDMP/remove',
				'view_model': {
					'Opts': [
						{'id': 'cookie', 'name': 'Cookie'},
						{'id': 'mobile', 'name': '移动设备ID'},
						{'id': 'ip', 'name': 'IP'}
					],
					'Type': 'cookie'
				}
			});
			this.$defType = 'cookie';
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var C = self.getConfig();
			el.append([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('人群标签')+'</span>',
					'<div>',
						'<label ms-repeat-item="Opts" class="cur_p mr15">',
							'<input ms-value="item.id" type="radio" ms-duplex-text="Type">',
							'<span class="ml5">{{item.name}}</span>',
						'</label>',
					'</div>',
					'<div class="P-peopleTagHeader">',
						LANG('禁用标签后，将不可在活动设置中重定向选项里找到被禁用的标签；同时之前使用了被禁用标签的活动，标签的定向条件也会失效'),
					'</div>',
				'</div>',
				'<div class="P-peopleTagGrid" pub-name="grid" pub-mod="admin/peopleTag/grid/grid.base"/>'
			].join(''));
			self.buildFromTemplate({
				'grid': {
					'hasSelect': true,
					'hasBatch': true,
					'hasSearch': true,
					'param': {
						'runstatus': 1,
						'Type': self.$defType
					}
				}
			});
			// 侧边栏过滤
			self.createAsync('sidebar', '@layout/v1/sidebar.base', {
				'target': C.targetSidebar,
				'items': [
					// 类型
					{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('类型：'),
						'data': [
							{ 'id': 0, 'name': LANG('所有') },
							{ 'id': 1, 'name': LANG('已启用') },
							{ 'id': 2, 'name': LANG('已禁用') }
						],
						'value': 1,
						'compareValue': true
					}}
				]
			});
			// 创建 禁用标签 弹窗
			self.createAsync('popDisabled', 'admin/peopleTag/popwin/popwin.popDisabled', {
				'layout': {
					'module': '@base/dialog.base',
					'mask': 1,
					'title': LANG('禁用标签提示'),
					'showFoot': true,
					'width': 600,
					'height': 500,
					'silence': false
				}
			});


			self.vm.watch('Type', function(newType)
			{
				self.reload({'Type': newType});
			});
		},
		// 操作列点击事件
		onOperateMenuShow: function( ev ) {
			var data = ev.param;
			var isDeleted = data['IsDeleted'];
			var op = null;

			if( isDeleted ) {
				if (pubjs.checkRight('pdmp.recover')) {
					op = {
						'id': 'enable',
						'name': LANG('启用')
					}
				}
			}
			else {
				if (pubjs.checkRight('pdmp.remove')) {
					op = {
						'id': 'disable',
						'name': LANG('禁用')
					}
				}
			}

			var userId = pubjs.getUserId();
			if (data.UserId != userId)
			{
				op = {
					'id': 'noOwn',
					'name': LANG('无权限修改该人群标签')
				}
			}

			ev.returnValue = [op];
			return false;
		},
		// 操作列选项点击事件
		onOperateMenuSelect: function( ev ) {
			var param = ev.param;
			var op = param[0]['key'];
			var data = param[1];
			var id = data['VisitorCateId'];
			var name = data['Name'];
			this.toggleStatus( id, op, name );
			return false;
		},
		//响应右边侧边栏选项
		onSidebarFilter: function( ev ) {
			var self = this
				,param = {};

			util.each(ev.param, function(item, idx){
				var type = idx;
				var id = item.id;
				switch (type) {
					case 'status':
						param.runstatus = +id;
					break;
				}
			});

			self.reload(param);
		},
		// 批量编辑
		onBatchShow: function(ev) {
			var result = [];

			if (pubjs.checkRight('pdmp.recover')) {
				result.push({id: 'recover', name: LANG('启用')});
			}
			if (pubjs.checkRight('pdmp.remove')) {
				result.push({id: 'remove', name: LANG('禁用')});
			}
			if(!result.length)
			{
				result.push({id: '', name: LANG('没有权限')});
			}

			ev.returnValue = result;
			return false;
		},
		// 批量操作-响应菜单操作
		onBatchSelect: function(ev){
			var self = this;
			var data = ev.param[0];
			var ids = ev.param[1].join();
			if (!ids) {
				pubjs.alert(LANG('请先选择人群标签'));
				return false;
			}

			var c = self.getConfig();
			switch( data.key ) {
				case 'recover':
					pubjs.confirm(
						LANG('确定启用这 %1 个标签吗？', ids.split(',').length),
						function( bool ) {
							if( bool ) {
								pubjs.mc.send(
									c.urlStart,
									{ 'tid': ids },
									self.onData.bind( self )
								);
							}
						});
					break;
				case 'remove':
					pubjs.confirm(
						LANG('确定禁用这 %1 个标签吗？', ids.split(',').length),
						function( bool ) {
							if( bool ) {
								pubjs.mc.send(
									c.urlPause,
									{ 'tid': ids },
									self.onData.bind( self )
								);
							}
						});
					break;
			}
			return false;
		},
		reload: function( param ) {
			this.$['grid'].setParam( param ).reload();
		},
		toggleStatus: function( id, op, name ) {
			var self = this;

			// 该标签不是该客户所拥有的，无权限修改。
			if (op == 'noOwn')
			{
				return false;
			}

			var runstatus = ( op === 'enable' ? 2 : 1 );
			if( runstatus === 2 ) {
				pubjs.confirm(
					LANG('确定启用') + LANG('标签：“') + name + LANG('”吗？'),
					function( bool ) {
					if( bool ) {
						pubjs.mc.send(
							self.getConfig('urlStart'),
							{ 'tid': id },
							self.onData.bind( self )
						);
					}
				});
			}
			else if( runstatus === 1 ) {
				var popwin = this.$['popDisabled'];
				popwin.reset().setTagInfo( id, name ).show();
			}
		},
		onData: function( err, data ) {
			if(err) {
				pubjs.error(err.message);
				return false;
			}
			this.$.grid.setSelectRowIds([]);
			this.reload();
		},
		onPauseTagOk: function( ev ) {
			var self = this;
			this.$['popDisabled'].hide();
			pubjs.mc.send(
				self.getConfig('urlPause'),
				{ 'tid': ev.param },
				this.onData.bind( self )
			);
			return false;
		}
	});
	exports.main = List;

	// 其他人群
	var BdxPeople = view.container.extend({
		init: function( config ) {
			config = pubjs.conf(config, {
				'class': 'P-peopleTag',
				'view_model': {
					'Opts': [
						{'id': 'pc', 'name': 'PC'},
						{'id': 'mobile', 'name': '移动'},
					],
					'Type': 'pc'
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var C = self.getConfig();
			el.append([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('其他人群标签')+'</span>',
					'<div>',
						'<label ms-repeat-item="Opts" class="cur_p ml15">',
							'<input ms-value="item.id" type="radio" ms-duplex-text="Type">',
							'<span class="ml5">{{item.name}}</span>',
						'</label>',
					'</div>',
				'</div>',
				'<div class="P-peopleTagGrid" pub-name="grid" pub-mod="admin/peopleTag/grid/grid.bdxPeople"/>'
			].join(''));
			self.buildFromTemplate({

			});


			self.vm.watch('Type', function(newType)
			{
				var isMobile = (newType == 'mobile');
				self.reload({'isMobile': isMobile});
			});
		},
		reload: function( param ) {
			this.$['grid'].setParam( param ).reload();
		}
	});
	exports.bdxPeople = BdxPeople;

	// 自有人群
	var BaiduPeople = view.container.extend({
		init: function( config ) {
			config = pubjs.conf(config, {
				'class': 'P-peopleTag',
				'view_model': {
					'Opts': [
						{'id': 'pc', 'name': 'PC'},
						{'id': 'mobile', 'name': '移动'},
					],
					'Type': 'pc'
				}
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var C = self.getConfig();
			el.append([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('自有人群标签')+'</span>',
					'<div>',
						'<label ms-repeat-item="Opts" class="cur_p ml15">',
							'<input ms-value="item.id" type="radio" ms-duplex-text="Type">',
							'<span class="ml5">{{item.name}}</span>',
						'</label>',
					'</div>',
				'</div>',
				'<div class="P-peopleTagGrid" pub-name="grid" pub-mod="admin/peopleTag/grid/grid.baiduPeople"/>'
			].join(''));
			self.buildFromTemplate({

			});

			self.vm.watch('Type', function(newType)
			{
				var channel_id = 10009;
				if (newType == 'mobile')
				{
					channel_id = 10024;
				}
				self.reload({'ChannelId': channel_id});
			});
		},
		reload: function( param ) {
			this.$['grid'].setParam( param ).reload();
		}
	});
	exports.baiduPeople = BaiduPeople;

	// 三方人群
	var TaobaoPeople = view.container.extend({
		init: function( config ) {
			config = pubjs.conf(config, {
				'class': 'P-peopleTag'
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var el = self.getDOM();
			var C = self.getConfig();
			el.append([
				'<div class="M-formHead">',
					'<span class="M-formHeadTitle">'+LANG('三方人群标签')+'</span>',
				'</div>',
				'<div class="P-peopleTagGrid" pub-name="grid" pub-mod="admin/peopleTag/grid/grid.taobaoPeople"/>'
			].join(''));
			self.buildFromTemplate({

			});
		},
		reload: function( param ) {
			this.$['grid'].setParam( param ).reload();
		}
	});
	exports.taobaoPeople = TaobaoPeople;
});